package com.fht.service.promotion.impl;

import com.fht.entity.PromotionSpecialOfferParticulars;
import com.fht.mapper.promotion.PromotionSpecialOfferParticularsMapper;
import com.fht.service.promotion.PromotionSpecialOfferParticularsService;
import com.fht.util.UnitConversionUtil;
import com.fht.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class PromotionSpecialOfferParticularsServiceImpl implements PromotionSpecialOfferParticularsService {

    @Resource
    public PromotionSpecialOfferParticularsMapper promotionSpecialOfferParticularsMapper;


    @Override
    public LayUITableVO<List<PromotionSpecialOfferParticulars>> findPageById(String id) {


        Example example = new Example(PromotionSpecialOfferParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        List<PromotionSpecialOfferParticulars> promotionSpecialOfferParticulars = promotionSpecialOfferParticularsMapper.selectByExample(example);

        for (PromotionSpecialOfferParticulars promotionSpecialOfferParticular : promotionSpecialOfferParticulars) {
            conversion(promotionSpecialOfferParticular);
        }
        LayUITableVO<List<PromotionSpecialOfferParticulars>> vo = new LayUITableVO<>();
        vo.setData(promotionSpecialOfferParticulars);


        return vo;
    }

    @Override
    @Transactional
    public void save(List<PromotionSpecialOfferParticulars> dataList) {

        for (PromotionSpecialOfferParticulars promotionSpecialOfferParticulars : dataList) {
            conversionOppose(promotionSpecialOfferParticulars);

            Example example = new Example(PromotionSpecialOfferParticulars.class);
            Example.Criteria c = example.createCriteria();
            c.andEqualTo("id",promotionSpecialOfferParticulars.getId());
            c.andEqualTo("recordId",promotionSpecialOfferParticulars.getRecordId());

            List<PromotionSpecialOfferParticulars> promotionSpecialOfferParticularsList = promotionSpecialOfferParticularsMapper.selectByExample(example);

            if (promotionSpecialOfferParticularsList.size()==0){
                promotionSpecialOfferParticularsMapper.insertSelective(promotionSpecialOfferParticulars);
            }else {
                promotionSpecialOfferParticularsMapper.updateByExampleSelective(promotionSpecialOfferParticulars,example);
            }


        }

    }

    @Override
    @Transactional
    public void deleteById(String id) {
        Example example = new Example(PromotionSpecialOfferParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        promotionSpecialOfferParticularsMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public void deleteByIdAndRecordId(String id, String recordId) {
        Example example = new Example(PromotionSpecialOfferParticulars.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("id",id);
        c.andEqualTo("recordId",recordId);
        promotionSpecialOfferParticularsMapper.deleteByExample(example);
    }


    public void conversion(PromotionSpecialOfferParticulars promotionSpecialOfferParticulars){

        promotionSpecialOfferParticulars.setSpecialPriceString(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getSpecialPrice()));
        promotionSpecialOfferParticulars.setDiscountString(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getDiscount()));
        promotionSpecialOfferParticulars.setPriceString(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getPrice()));

    }
    public void conversionOppose(PromotionSpecialOfferParticulars promotionSpecialOfferParticulars){

        promotionSpecialOfferParticulars.setSpecialPrice(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getSpecialPriceString()));
        promotionSpecialOfferParticulars.setDiscount(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getDiscountString()));
        promotionSpecialOfferParticulars.setPrice(UnitConversionUtil.conversion(promotionSpecialOfferParticulars.getPriceString()));

    }

}
